﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using AwesomeDataLager.DummaDTO;

namespace AwesomeDataLager
{
   public static class GetDBInfo
    {
       public static Object GetGameSessionListInfoDB(string namn)
       {
           
           var game = new GameDbEntities().GameSession;

           return
               game.Select(ss => new GameSessionDTO {NumbersOfRounds = (int) ss.NoOfGameRounds, Id = ss.ID, GameId = (int) ss.GameID}).ToList();
       }

       public static List<GameDto> GetGameListInfoDb()
       {
           var game = new GameDbEntities().Game;

           return game.Select(g => new GameDto{ Id = g.ID, Name = g.Name }).ToList();

       }

       public static List<PlayerDto> GetGameListInfoDB()
       {
           var game = new GameDbEntities().Player;

           return game.Select(s => new PlayerDto {Id = s.ID, Name = s.Name}).ToList();
       }

       /// <summary>
       /// Returns a player Spelare by Id
       /// </summary>
       /// <param name="id"></param>
       /// <returns>Spelare</returns>
       public static Player GetPlayerById(int id)
       {
           try
           {
               var allPlayers = new GameDbEntities().Player;

               var player = allPlayers.Where(s => s.ID == id).SingleOrDefault();
               return player;
           }
           catch
           {
               return null;
           }
       }

       public static Game GetGameById(int id)
       {
           try
           {
               var allGames = new GameDbEntities().Game;
               //var game = allGames.Select(w => w.ID == id).SingleOrDefault();
               var game = allGames.Where(g => g.ID == id).SingleOrDefault();
               return game;
           }
           catch
           {
               return null;
           }
       }

       public static PlayerDto DetermineRoundWinner(List<PlayerChoice> playerChoices)
       {
           var playerOneChoice = playerChoices[0].Choice;
           var playerTwoChoice = playerChoices[1].Choice;
           
           //temp solution
           if (playerOneChoice > playerTwoChoice)
           {
               
           }

           return null;
       }

       public static PlayerDto DetermineSessionWinner(int gameSessionId)
       {
           var playerChoices = new List<PlayerChoice>();
           var entities = new GameDbEntities();

           var gameRoundId = entities.GameRound.FirstOrDefault(x => x.GameSessionID == gameSessionId).ID;

           playerChoices = entities.PlayerChoice.Where(x => x.GameRoundID == gameRoundId).ToList();

           var result = DetermineRoundWinner(playerChoices);

           return null;
       }

       /// <summary>
       /// Login the player
       /// </summary>
       /// <param name="login">Players username</param>
       /// <param name="password">Players password</param>
       /// <returns>Returns <see cref="PLayerDto"/> with players details</returns>
       public static PlayerDto LoginPlayer(string login, string password)
       {
           var entities = new GameDbEntities();

           var player = entities.Player.FirstOrDefault(p => p.Name == login);

           if (player == null)
           {
               return null;
           }

           return new PlayerDto
           {
               Id = player.ID,
               Name = player.Name
           };
       }

       public static GameSession GetGameSessionById(int sessionId)
       {
           try
           {
               var session = new GameDbEntities();

               GameSession sess = session.GameSession.Where(s => s.ID == sessionId).FirstOrDefault();
               return sess;
           }
           catch
           {
               return null;
           }
           //throw new NotImplementedException();
       }

       public static bool AddPlayerSession(int playerId, int sessionId)
        {
            try
            {
                //var player = GetDBInfo.GetPlayerById(playerId);
                //var session = GetDBInfo.GetGameSessionById(sessionId);
                if (SaveDBInfo.AddPlayerSession(playerId, sessionId))
                {
                    return true;
                }

                else
                return false;
            }
            catch
            {
                return false;
            }
        }

       public static bool MakePlayerChoice(int gameSessionId, int playerId, int playerChoiceId)
        {
            return SaveDBInfo.MakePlayerChoice(gameSessionId, playerId, playerChoiceId);
        }

       public static List<GameSessionDTO> GetActiveGameSessionInfoDb()
       {

           var session = new GameDbEntities().GameSession.Where(gs => gs.ActiveGame == 1);

           return
               session.Select(
                   gsd =>
                   new GameSessionDTO
                   {
                       NumbersOfRounds = (int)gsd.NoOfGameRounds,
                       Id = gsd.ID,
                       GameId = (int)gsd.GameID
                   }).ToList();
       }

       public static List<GameSessionDTO> GetJoinableGameSessions(int gameID)
       {
           var session = new GameDbEntities().GameSession.Where(gs => gs.Winner == null && gs.ActiveGame == 0 && gs.GameID == gameID);

           return
               session.Select(
                   gsd =>
                   new GameSessionDTO
                   {
                       NumbersOfRounds = (int)gsd.NoOfGameRounds,
                       Id = gsd.ID,
                       GameId = (int)gsd.GameID
                   }).ToList();
       }

       //TODO Refactor and CleanUp! - Late Sundaynight slacking
       public static List<GameSessionDTO> GetPlayersGameSessions(int playerId)
       {
           var GameSessions = new GameDbEntities().GameSession;
           var PlayerSessions = new GameDbEntities().PlayerGameSession;

           var test = PlayerSessions.Where(x => x.PlayerID == playerId);
           List<GameSessionDTO> newList = new List<GameSessionDTO>();

           foreach (var Psession in test)
           {
               foreach (var Gsession in GameSessions)
               {
                   if (Psession.GameSessionID == Gsession.GameID)
                   {
                       // TODO om NoOfGameRounds ska inte kunna vara null!! Fixa / Sätter till 1 om det är null i nuläget
                       newList.Add(new GameSessionDTO { GameId = (int)Gsession.GameID, Id = Gsession.ID , NumbersOfRounds = Gsession.NoOfGameRounds ?? 1});
                   }
               }
           }

           return newList;
       }
    }
}
